package com.embracesource.monitor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.embracesource.monitor.entity.Database;
import com.embracesource.monitor.mapper.DatabaseMapper;
import com.embracesource.monitor.service.IDatabaseService;
import com.gnerv.pabu.basic.core.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 基础平台-服务器表 服务实现类
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-09-03
 */
@Service
public class DatabaseServiceImpl extends ServiceImpl<DatabaseMapper, Database> implements IDatabaseService {

    @Resource
    private DatabaseMapper databaseMapper;

    @Override
    public boolean saveDatabase(Database database) {
        if (StringUtils.isEmpty(database.getUkId())) {
            createDatabase(database);
        } else {
            updateDatabase(database);
        }
        return true;
    }

    @Override
    public boolean createDatabase(Database database) {
        database.setUkId(UUIDUtils.getLower());
        databaseMapper.insert(database);
        return true;
    }

    @Override
    public boolean deleteDatabase(String databaseId) {
        databaseMapper.deleteById(databaseId);
        return true;
    }

    @Override
    public boolean updateDatabase(Database database) {
        databaseMapper.updateById(database);
        return true;
    }

    @Override
    public Database selectDatabase(String databaseId) {
        Database database = databaseMapper.selectById(databaseId);
        return database;
    }

    @Override
    public Page<Database> listDatabase(Database database, Long current, Long size) {
        Page<Database> databases = databaseMapper.listDatabase(new Page<>(current, size), database);
        return databases;
    }
}
